#include "lame.h"
#include "com_huaxin_common_message_mp3recorder_util_LameUtil.h"

#ifdef ANDROID
#define LOGE(format, ...)  __android_log_print(ANDROID_LOG_ERROR, "(>_<)", format, ##__VA_ARGS__)
#define LOGI(format, ...)  __android_log_print(ANDROID_LOG_INFO,  "(=_=)", format, ##__VA_ARGS__)
#else
#define LOGE(format, ...)  printf("(>_<) " format "\n", ##__VA_ARGS__)
#define LOGI(format, ...)  printf("(^_^) " format "\n", ##__VA_ARGS__)
#endif

static lame_global_flags *lame = NULL;
/*
static hip_global_flags *hip;
//static mpg123_handle *m;

JNIEXPORT void JNICALL Java_com_huaxin_common_message_mp3recorder_util_LameUtil_initdecode
  (JNIEnv *env, jclass cls){
      if(hip != NULL){
        hip_decode_exit(hip);
        hip = NULL;
      }
      hip = hip_decode_init();
      if (!hip)
      {
      	LOGI("创建mp3解码失败");
      	return -1;
      }else{
        LOGI("创建mp3解码成功");
      }
      return 1;
}

JNIEXPORT jint JNICALL Java_com_huaxin_common_message_mp3recorder_util_LameUtil_decode
  (JNIEnv *env, jclass cls, jbyteArray mp3_buf, jint bufsize, jshortArray pcm_buf){

    const jsize pcmbuf_size = (*env)->GetArrayLength(env, pcm_buf);
  	jbyte* j_mp3buf = (*env)->GetByteArrayElements(env, mp3_buf, NULL);
  	jshort* j_pcmbuf = (*env)->GetShortArrayElements(env, pcm_buf, NULL);

    int samples;
    short pcm_l[pcmbuf_size];
    short pcm_r[pcmbuf_size];
    memset(pcm_l, 0 , pcmbuf_size);
    memset(pcm_r, 0 , pcmbuf_size);

    samples = hip_decode(hip, j_mp3buf, bufsize, pcm_l, pcm_r);

int i ;
    for( i= 0; i < samples; i ++)
    {
       j_pcmbuf[i] = (pcm_l[i] + pcm_r[i])/2;
    }

    (*env)->ReleaseByteArrayElements(env, mp3_buf, j_mp3buf, 0);
    (*env)->ReleaseShortArrayElements(env, pcm_buf, j_pcmbuf, 0);

    return samples;
}

JNIEXPORT void JNICALL Java_com_huaxin_common_message_mp3recorder_util_LameUtil_closedecode
  (JNIEnv *env, jclass cls){
    hip_decode_exit(hip);
    hip = NULL;
    LOGI("释放mp3解码资源");
}
*/
JNIEXPORT void JNICALL Java_com_huaxin_common_message_mp3recorder_util_LameUtil_init(
		JNIEnv *env, jclass cls, jint inSamplerate, jint inChannel, jint outSamplerate, jint outBitrate, jint quality) {
	if (lame != NULL) {
		lame_close(lame);
		lame = NULL;
	}
	lame = lame_init();
	lame_set_in_samplerate(lame, inSamplerate);
	lame_set_num_channels(lame, inChannel);//输入流的声道
	lame_set_out_samplerate(lame, outSamplerate);
	lame_set_brate(lame, outBitrate);
	lame_set_quality(lame, quality);
	lame_init_params(lame);
}

JNIEXPORT jint JNICALL Java_com_huaxin_common_message_mp3recorder_util_LameUtil_encode(
		JNIEnv *env, jclass cls, jshortArray buffer_l, jshortArray buffer_r,
		jint samples, jbyteArray mp3buf) {
	jshort* j_buffer_l = (*env)->GetShortArrayElements(env, buffer_l, NULL);

	jshort* j_buffer_r = (*env)->GetShortArrayElements(env, buffer_r, NULL);

	const jsize mp3buf_size = (*env)->GetArrayLength(env, mp3buf);
	jbyte* j_mp3buf = (*env)->GetByteArrayElements(env, mp3buf, NULL);

	int result = lame_encode_buffer(lame, j_buffer_l, j_buffer_r,
			samples, j_mp3buf, mp3buf_size);

	(*env)->ReleaseShortArrayElements(env, buffer_l, j_buffer_l, 0);
	(*env)->ReleaseShortArrayElements(env, buffer_r, j_buffer_r, 0);
	(*env)->ReleaseByteArrayElements(env, mp3buf, j_mp3buf, 0);

	return result;
}

JNIEXPORT jint JNICALL Java_com_huaxin_common_message_mp3recorder_util_LameUtil_flush(
		JNIEnv *env, jclass cls, jbyteArray mp3buf) {
	const jsize mp3buf_size = (*env)->GetArrayLength(env, mp3buf);
	jbyte* j_mp3buf = (*env)->GetByteArrayElements(env, mp3buf, NULL);

	int result = lame_encode_flush(lame, j_mp3buf, mp3buf_size);

	(*env)->ReleaseByteArrayElements(env, mp3buf, j_mp3buf, 0);

	return result;
}

JNIEXPORT void JNICALL Java_com_huaxin_common_message_mp3recorder_util_LameUtil_close
(JNIEnv *env, jclass cls) {
	lame_close(lame);
	lame = NULL;
}
